package com.qs.redis.jedis.lock;

import java.util.concurrent.TimeUnit;

public class TestLock {

    public static void main(String[] args) {
        DistributeLock lock = new DistributeLock();
        Task task = new Task(lock);
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(task, "thread-" + i);
        }


        for(Thread thread : threads){
            thread.start();
        }
    }


    static class Task implements Runnable {

        private DistributeLock lock;

        public Task(DistributeLock lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            String identify = this.lock.acquireLock("lock", 5000, 3000);
            try {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                //释放锁
                this.lock.releaseLock("lock", identify);
            }
        }
    }
}
